package com.zhj.network.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @PackageName:com.zhj.network.udp
 * @ClassName:UdpServerDemo01
 * @auter: 朱慧杰
 * @date:2020/7/21 0021 13:34
 **/
public class UdpServerDemo01 {

    // 等待客户端的消息
    public static void main(String[] args) throws Exception {
        // 1. 开放端口
        DatagramSocket socket = new DatagramSocket(9090);

        // 2. 接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        // 3. 接收包
        socket.receive(packet);

        System.out.println(packet.getAddress().getHostName());
        System.out.println(new String(packet.getData(), 0, packet.getLength()));

        socket.close();
    }

}
